<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :page.sync="page"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getDeptList,
  removeDept,
  submitDept,
  getDept,
  getDeptTree,
} from "@/api/system";

  export default {
    data() {
      return {
        form: {},
        selectionList: [],
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0,
        },
        parentTreeData: [],
        data: []
      };
    },
    computed: {
      option() {
      return {
          lazy: true,
          tip: false,
          simplePage: true,
          searchShow: true,
          searchMenuSpan: 6,
          tree: true,
          border: true,
          // index: true,
          selection: true,
          viewBtn: true,
          menuWidth: 300,
          dialogClickModal: false,
        column: [
          {
            label: "Id",
            prop: "id",
            display: false,
            hide: true,
          },
          {
            label: "部门名称",
            prop: "deptName",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入部门名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "部门全称",
            prop: "fullName",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入部门全称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "上级部门",
            prop: "parentId",
            dicData: this.parentTreeData,
            type: "tree",
            // hide: true,
            props: {
              label: "title",
            },
            rules: [
              {
                required: false,
                message: "请选择上级部门",
                trigger: "click",
              },
            ],
          },

          {
            label: "排序",
            prop: "sort",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入排序",
                trigger: "blur",
              },
            ],
          },

          {
            label: "备注",
            prop: "remark",
            span: 24,
            rules: [
              {
                required: false,
                message: "请输入备注",
                trigger: "blur",
              },
            ],
          },
        ],
      };
    },
      
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    created(){
      this.getParentTreeData();
    },
    methods: {
      getParentTreeData() {
        getDeptTree().then((res) => {
          const data = res.data.data;
          this.parentTreeData = data;
        });
      },
      
      rowSave(row, done, loading) {
        submitDept(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, () => {
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        submitDept(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          // 数据回调进行刷新
          done();
        }, () => {
          loading();
        });
      },
      rowDel(row, index, done) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return removeDept(row.id);
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.onLoad(this.page);
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return removeDept(this.ids);
          })
          .then(() => {
            // 刷新表格数据并重载
            this.onLoad(this.page);
            this.$refs.crud.toggleSelection();
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      beforeOpen(done, type) {
        if (["add", "edit"].includes(type)) {
          this.getParentTreeData();
        }
        if (["edit", "view"].includes(type)) {
          getDept(this.form.id).then((res) => {
            this.form = res.data.data;
          });
        }
        done();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getDeptList(page.currentPage,page.pageSize,Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
    }
  };
</script>

<style>
</style>
